//
//  ViewController.m
//  3DtouchDemo
//
//  Created by liuyang on 16/1/21.
//  Copyright © 2016年 liuyang. All rights reserved.
//

#import "ViewController.h"
#import "PopViewController.h"

@interface ViewController ()<UIViewControllerPreviewingDelegate>{
    UIImageView *_imageView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"首页12345";
    self.view.backgroundColor = [UIColor yellowColor];

    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    _imageView.image = [UIImage imageNamed:@"a43180679d4aafa0e37560c43c9a6233.jpg"];
    [self.view addSubview:_imageView];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];
    button.backgroundColor = [UIColor greenColor];
    [self.view addSubview:button];

    if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0){
        //3D Touch
        // Check for force touch feature, and add force touch/previewing capability.
        if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
            //Register for `UIViewControllerPreviewingDelegate` to enable "Peek" and "Pop".
            //The view controller will be automatically unregistered when it is deallocated.
            [self registerForPreviewingWithDelegate:self sourceView:self.view];
        }
    }
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - UIViewControllerPreviewingDelegate 3D Touch Peek and Pop

- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
    if(viewControllerToCommit){
        [self showViewController:viewControllerToCommit sender:self];
    }
}


#pragma mark - UIViewControllerPreviewingDelegate 3D Touch Peek and Pop
- (UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    UIImageView *peekView = nil;
    NSInteger peekIndex = -1;
    CGPoint pointContent = [self.view convertPoint:location toView:_imageView];
    peekView = [[UIImageView alloc]initWithFrame:CGRectMake(pointContent.x, pointContent.y, _imageView.frame.size.width, _imageView.frame.size.height)];
    previewingContext.sourceRect = [peekView convertRect:peekView.bounds toView:self.view];
    
    PopViewController *vc = [[PopViewController alloc] initWithSCEvent:_imageView.image attachmentIndex:peekIndex];
    
    return vc;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
